﻿package com.tvein.sanguo.swc.action.command
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	import fl.controls.Slider;
	import fl.controls.dataGridClasses.*;
	import fl.controls.DataGrid;
	import fl.data.DataProvider;
	import fl.events.ListEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class Command9 extends MovieClip
	{
		public static const NAME:String = "Command-9";
		
		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var targetID:int;
		private var modeList:DataGrid;
		private var timeSlider:Slider;
		private var paySlider:Slider;
		
		private var productID:int;
		private var strResult:String;
		private var unitTime:int;
		private var maxCount:int;
		private var option:int;
		
		public function Command9()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			txtGoods.border = true;
			txtGoods.borderColor = 0x333333;
			
			modeList = new DataGrid();
			modeList.x = 5;
			modeList.y = 70;
			modeList.width = 590;
			modeList.height = 110;
			this.addChild(modeList);
			
			timeSlider = new Slider();
			timeSlider.x = 250;
			timeSlider.y = 260;
			timeSlider.width = 240;
			timeSlider.height = 3;
			timeSlider.maximum = 100;
			timeSlider.minimum = 0;
			timeSlider.snapInterval = 10;
			timeSlider.tickInterval = 10;
			timeSlider.enabled = false;
			this.addChild(timeSlider);
			
			paySlider = new Slider();
			paySlider.x = 250;
			paySlider.y = 330;
			paySlider.width = 240;
			paySlider.height = 3;
			paySlider.maximum = 100;
			paySlider.minimum = 0;
			paySlider.snapInterval = 10;
			paySlider.tickInterval = 10;
			paySlider.enabled = false;
			this.addChild(paySlider);
			
			btnStart.visible = false;
			btnTimeMinus.visible = false;
			btnTimePlus.visible = false;
			btnPayMinus.visible = false;
			btnPayPlus.visible = false;
			
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnStart.addEventListener(MouseEvent.CLICK, onClick);
			btnTimeMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnTimePlus.addEventListener(MouseEvent.CLICK, onClick);
			btnPayMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnPayPlus.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setTargetId":
					targetID = params.TargetId;
					break;
				case "setData":
					setData(params);
					reRoad();
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			var style:TextFormat = new TextFormat();
			style.color = 0xff6600;
			style.size = 16;
			style.font = "LiSu";
			
			modeList.setRendererStyle("textFormat", style);
			modeList.addEventListener(ListEvent.ITEM_CLICK, onSelected);
			
			var c0:DataGridColumn = new DataGridColumn("ID");
			var c1:DataGridColumn = new DataGridColumn("Option");
			var c2:DataGridColumn = new DataGridColumn("需要");
			var c3:DataGridColumn = new DataGridColumn("获得");
			var c4:DataGridColumn = new DataGridColumn("单位时间");
			var c5:DataGridColumn = new DataGridColumn("最大可用");
			
			c0.visible = false;
			c1.visible = false;
			c2.width = 200;
			c3.width = 200;
			c4.width = 90;
			c5.width = 100;
			
			modeList.addColumn(c0);
			modeList.addColumn(c1);
			modeList.addColumn(c2);
			modeList.addColumn(c3);
			modeList.addColumn(c4);
			modeList.addColumn(c5);
			
			for (var i:int = 0; i < params.length; i++)
			{
				modeList.addItem({"ID":params[i].ID, "option":params[i].option, "需要":params[i].material, "获得":params[i].product, "单位时间":params[i].unitTime, "最大可用":params[i].maxCount});
			}
		}
		
		private function reRoad():void
		{			
			if (timeSlider.value == 0)
			{
				txtTime.text = "";
				btnTimeMinus.visible = false;
			}
			else
			{
				btnTimeMinus.visible = true;
			}
			
			if (timeSlider.value < 100)
			{
				btnTimePlus.visible = true;
			}
			else
			{
				btnTimePlus.visible = false;
			}
			
			if (paySlider.value == 0)
			{
				txtPay.text = "";
				btnPayMinus.visible = false;
			}
			else
			{
				btnPayMinus.visible = true;
			}
			
			if (paySlider.value < 100)
			{
				btnPayPlus.visible = true;
			}
			else
			{
				btnPayPlus.visible = false;
			}
			
			if (txtGoods.text == "")
			{
				btnTimeMinus.visible = false;
				btnTimePlus.visible = false;
				btnPayMinus.visible = false;
				btnPayPlus.visible = false;
			}
			else
			{
				var tmpResult:String = "";
				
				var resultsArray:Array = strResult.split(",");
				
				for (var i:int = 0; i < resultsArray.length; i++)
				{
					var resultArray:Array = resultsArray[i].split(" ");
					
					for (var j:int = 0; j < resultArray.length; j++)
					{
						if (resultArray[j].match("^[1-9]\d*$"))
						{
							resultArray[j] = resultArray[j] * timeSlider.value * maxCount / 10;
						}

						tmpResult = tmpResult + resultArray[j] + " ";
					}
				}
				
				txtGoods.text = tmpResult;
			}
			
			txtTime.text = "（" + timeSlider.value * unitTime * maxCount / 10 + " 人时）";
			txtPay.text = "（" + paySlider.value + " 钱）";

			if ((txtGoods.text != "") && (timeSlider.value > 0) && (paySlider.value > 0))
			{
				btnStart.visible = true;
			}
			else
			{
				btnStart.visible = false;
			}
		}
		
		private function onSelected(evt:ListEvent):void
		{
			productID = evt.item["ID"];
			strResult = evt.item["获得"];
			unitTime = evt.item["单位时间"];
			maxCount = evt.item["最大可用"];
			option = evt.item["option"];
			
			if (maxCount > 0)
			{
				txtGoods.text = strResult;
			}
			else
			{
				txtGoods.text = "";
			}
			
			timeSlider.value = 0;
			paySlider.value = 0;
			
			reRoad();
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "Command-9"]));
					break;
				case "btnStart":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["SkillCall(30,SkillUse," + targetID + ",productId=" + productID + "&count=" + maxCount + "&timeCount=" + timeSlider.value * unitTime * maxCount / 10 + "&wage=" + paySlider.value + "&option=" + option + ");"]]));
					break;
				case "btnTimeMinus":
					timeSlider.value -= 10;
					reRoad();
					break;
				case "btnTimePlus":
					timeSlider.value += 10;
					reRoad();
					break;
				case "btnPayMinus":
					paySlider.value -= 10;
					reRoad();
					break;
				case "btnPayPlus":
					paySlider.value += 10;
					reRoad();
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnStart.removeEventListener(MouseEvent.CLICK, onClick);
			btnTimeMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnTimePlus.removeEventListener(MouseEvent.CLICK, onClick);
			btnPayMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnPayPlus.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
	}
}